package com.gc.cscott.verify.contest2012jan;

import com.gc.cscott.verify.JavaTaskOutputVerifier;
import com.gc.cscott.verify.OutputVerifier.VerifiedOutput;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author User
 */
public class CheckZ6Supper implements JavaTaskOutputVerifier {

    public VerifiedOutput verify(String inputText, String outputText, String validationHintText) {
        final long ditios;
        final long mans;
        final List<Long> options;
        {
            final List<Long> rawInput = toList(inputText);
            ditios = rawInput.get(0);
            mans = rawInput.get(1);
            options = new ArrayList<Long>(rawInput.subList(2, rawInput.size()));
            if (options.size()!=mans){
                throw new IllegalArgumentException("Wrong input!!!: "+inputText);
            }
        }
        final List<Long> solution;
        {
            try {
                solution = toList(outputText);
            } catch(Exception e){
                return VerifiedOutput.outputIsWrong("Failed: "+e);
            }
        }
        int index = 0;
        System.out.println(inputText);
        for(long manOptions:options){
            index ++;
            final long currentOptions = product(solution);
            //System.out.println(""+solution+" -> "+currentOptions);
            if (manOptions!=currentOptions){
                final String error = "Expected "+manOptions+" for man("+index+") but holds: "+currentOptions;
                //System.out.println(error);
                return VerifiedOutput.outputIsWrong(error);
            } else {
                if (index<mans){
                    final long nextOptions = options.get(index);
                    boolean stillFine = false;
                    for(int i=0;i<solution.size();i++){
                        if (solution.get(i)>0){
                            solution.set(i,solution.get(i)-1);
                            {
                                if(nextOptions==product(solution)){
                                    stillFine = true;
                                    break;
                                }
                            }
                            solution.set(i,solution.get(i)+1);
                        }
                    }
                    if (!stillFine){
                        final String error = "Expected position with: "+nextOptions+" ditios at step "+index;
                        //System.out.println(error);
                        return VerifiedOutput.outputIsWrong(error);
                    }
                }
            }
        }
        //System.out.println("Passed");
        return VerifiedOutput.outputIsCorrect();
    }

    private long product(List<Long> vector){
        long result = 1;
        for(long value:vector){
            result*=value;
        }
        return result;
    }

    private List<Long> toList(String input){
        final List<Long> result = new ArrayList<Long>();
        for(final String item:input.trim().split("\\s+")){
            final String trim = item.trim();
            if (trim.isEmpty()==false){
                result.add(Long.parseLong(trim));
            }
        }
        return result;
    }
}
